package core.subcontrollers;

import boundary.UI;
import dto.Player;
import entity.Account;
import entity_controllers.Option;
import ie.FieldBoard;
import ie.PlayerBoard;
import ie.Shaker;

public class Rules {

	public static final int PASSED_START_AMMOUND = 4000;

	public static boolean hasWinner(PlayerBoard playerBoard) {
		int count = 0;
		for (int i = 0; i < playerBoard.getPlayers().length; i++) {
			System.out.println("hasWinner " + playerBoard.getPlayers()[i].getName() + " isBankrupt " + ((Account) playerBoard.getPlayers()[i].getAccount()).isBankrupt());
			if (!((Account) playerBoard.getPlayers()[i].getAccount()).isBankrupt())
				count++;
		}
		System.out.println("hasWinner count: " + count);
		boolean hasWinner = count == 1;
		if (hasWinner)
			System.out.println("There is a winner!");
		else
			System.out.println("No winner.");
		return hasWinner;
	}

	public static void setBankrupt(Player debiter, Player creditor) {
		Account account = (Account) debiter.getAccount();
		int balance = account.getBalance();
	
		// give properties to creditor
		// remove car
		debiter.setFieldIndex(0);
		account.subBalance(balance);
	
		if (creditor != null) {
			creditor.getAccount().addBalance(balance);
	
			Option[] options = account.getOM().getAvailableOptions();
	
			for (int i = 0; i < options.length; i++) {
				options[i].disable();
			}
			options = account.getEM().getAvailableOptions();
	
			for (int i = 0; i < options.length; i++) {
				options[i].disable();
			}
		}
		account.setBankrupt(true);
		UI.get().showPlayer(debiter);
		UI.get().showPlayer(creditor);
		UI.get().setBankrupt(debiter);
		UI.get().getMessage("Du kunne ikke betale, du er gået bankerot!");
	
	}

}
